'use strict';
module.exports = {
	/**
	 * 此函数名称
	 * @url user/pub/test1 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid
		} = data;
		let res = {
			code: 0,
			msg: "",
			data: {}
		};
		// 业务逻辑开始-----------------------------------------------------------
		let {
			monthStart,
			monthEnd,
			yearStart,
			yearEnd,
		} = vk.pubfn.getCommonTime(new Date());
		console.log(vk.pubfn)
		let times = vk.pubfn.getOffsetTime(new Date(), {
			year: 1,
			month: 0,
			day: 0,
			hours: 0,
			minutes: 0,
			seconds: 0,
			mode: "before", // after 之后 before 之前
		});


		let qntime = vk.pubfn.getCommonTime(new Date(times))

		// let requestRes = await vk.request({
		// 	url: `https://www.tianqiapi.com/api/`,
		// 	method: "GET",
		// 	data: {
		// 		appid: '63642244',
		// 		appsecret: 'kUxZk9Nh',
		// 		version: 'v9',
		// 		city: '合肥'
		// 	},
		// });
		let requestRes = {
			data: [{
				wea: '-',
				tem2: '-',
				tem1: '-',
				air_tips: '-'
			}]
		}
		res.data.wea =
			`今日${requestRes.data[0].wea}，${requestRes.data[0].tem2}℃ -${requestRes.data[0].tem1}℃，${requestRes.data[0].air_tips}`
		console.log(requestRes)
		// 总用户
		// res.data.total_user = await vk.baseDao.count({
		// 	dbName: "uni-id-users"
		// })

		// 今天日期
		let today = new Date(new Date().toLocaleDateString()).getTime();

		// 今日客户订单
		res.data.t_c_order = await vk.baseDao.count({
			dbName: "uni-id-base-order",
			whereJson: {
				status: 2,
				_add_time: _.and(_.gt(today), _.lt(today + 24 * 60 * 60 * 1000))
			}
		})
		// 昨日客户订单
		res.data.y_c_order = await vk.baseDao.count({
			dbName: "uni-id-base-order",
			whereJson: {
				status: 2,
				_add_time: _.and(_.gt(today - 24 * 60 * 60 * 1000), _.lt(today))
			}
		})
		// 下单客户总数
		res.data.a_c_order = await vk.baseDao.count({
			dbName: "uni-id-base-order",
			status: 2,
		})

		// 今日下单用户数
		res.data.t_s_order = await vk.baseDao.count({
			dbName: "uni-id-base-order",
			whereJson: {
				status: 2,
				_add_time: _.and(_.gt(today), _.lt(today + 24 * 60 * 60 * 1000))
			},
			groupJson: {
				_id: "$user_id", // _id是分组id， $ 后面接字段名
			},
		})
		// 昨日下单用户数
		res.data.y_s_order = await vk.baseDao.count({
			dbName: "uni-id-base-order",
			whereJson: {
				status: 2,
				_add_time: _.and(_.gt(today - 24 * 60 * 60 * 1000), _.lt(today))
			},
			groupJson: {
				_id: "$user_id", // _id是分组id， $ 后面接字段名
			},
		})
		// 总下单用户数
		res.data.a_s_order = await vk.baseDao.count({
			dbName: "uni-id-base-order",
			whereJson: {
				status: 2,
			},
			groupJson: {
				_id: "$user_id", // _id是分组id， $ 后面接字段名
			},
		})

		// 今年交易额度
		//正常订单金额

		res.data.t_g_money = await vk.baseDao.sum({
			dbName: "uni-id-base-order",
			fieldName: "total_fee",
			whereJson: {
				status: 2,
				_add_time: _.and(_.gt(today), _.lt(today + 24 * 60 * 60 * 1000))
			}
		})




		// 去年交易额度
		//正常订单金额
		res.data.y_g_money = await vk.baseDao.sum({
			dbName: "uni-id-base-order",
			fieldName: "total_fee",
			whereJson: {
				status: 2,
				_add_time: _.and(_.gt(today - 24 * 60 * 60 * 1000), _.lt(today))
			}
		})
		//退货交易金额


		//总交易额


		res.data.a_g_money = await vk.baseDao.sum({
			dbName: "uni-id-base-order",
			fieldName: "total_fee",
			whereJson: {
				status: 2
			}
		})



		//总用户
		res.data.a_g_user = await vk.baseDao.count({
			dbName: "uni-id-users",
			whereJson: {

			}
		});

		//今日新注册
		res.data.t_d_user = await vk.baseDao.count({
			dbName: "uni-id-users",
			whereJson: {
				register_date: _.and(_.gt(today), _.lt(today + 24 * 60 * 60 * 1000))
			}
		});


		//今日新注册
		res.data.y_d_user = await vk.baseDao.count({
			dbName: "uni-id-users",
			whereJson: {
				register_date: _.and(_.gt(today - 24 * 60 * 60 * 1000), _.lt(today))

			}
		});


		// 正常订单待审核数量

		// 回款折线图表
		// let lastMonth = vk.pubfn.getDayOffsetStartAndEnd(-31).startTime
		// let step = 24 * 60 * 60 * 1000
		// let map = {
		// 	_add_time: _.gt(lastMonth)
		// }
		// let obj = await db.collection('orders-received').where(map).field().get()
		// const orderList = obj.data
		// const dateList = []
		// for (let i = lastMonth; i <= today; i += step) {
		// 	let payArr = orderList.filter(item => {
		// 		return item._add_time >= i && item._add_time < i + step
		// 	}).map(item => {
		// 		return item.rebate_amount
		// 	})

		// 	dateList.push({
		// 		_id: vk.pubfn.timeFormat(i, "MM-dd"),
		// 		sum: payArr.length > 0 ? +payArr.reduce((a, b) => a + b).toFixed(2) : 0
		// 	})
		// }
		// res.chartData = dateList
		// 回款饼图
		// res.pieData = await vk.baseDao.selects({
		// 	dbName: "orders-received",
		// 	getMain: true,
		// 	pageIndex: 1,
		// 	pageSize: -1,
		// 	whereJson: {
		// 		status: _.exists(false)
		// 	},
		// 	groupJson: {
		// 		_id: "$company_name",
		// 		sum: _.$.sum("$rebate_amount")
		// 	}
		// })

		// // 平台金额（初始金额 + 订单回款 + 开支 + 其他回款）
		// let base_money = await vk.baseDao.sum({
		// 	dbName: "base-money",
		// 	fieldName: "number"
		// })
		// let daily_expenses = await vk.baseDao.sum({
		// 	dbName: "daily-expenses",
		// 	fieldName: "total"
		// })
		// let payback_record = await vk.baseDao.sum({
		// 	dbName: "payback-record",
		// 	fieldName: "total"
		// })
		// res.data.plant_total = base_money + res.data.a_g_money + daily_expenses + payback_record

		// // 账号余额
		// res.data.account = await vk.baseDao.sum({
		// 	dbName: "account-balance",
		// 	fieldName: "balance"
		// })

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
